﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Hr;
using ESOL.Common.Entity;
using ESOL.Common;

namespace ESOL.WinUI.Hr
{
    public partial class frmManageEmployeePositionApplication : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private HrPersonPositionApplicationEntity mSelectedPersonPositionApp;
        #endregion

        #region Constructor
        public frmManageEmployeePositionApplication()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity department = (MstDepartmentEntity)txtDepartment.Tag;
                condition += " AND PPA.DEPARTMENT_ID = '" + department.DepartmentId + "'";
            }
            if (mCanQueryOtherPerson)
            {
                if (txtPerson.Tag != null && txtPerson.Tag.GetType() == typeof(MstPersonEntity))
                {
                    MstPersonEntity person = (MstPersonEntity)txtPerson.Tag;
                    condition += " AND PPA.PROMOTION_PERSON = '" + person.PersonId + "'";
                }
            }
            else
            {
                condition += " AND PPA.APPLICATION_PERSON = '" + CommonParameter.CurrentUser.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND PPA.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (dtpApplicationDate.EditValue!=null)
            {
                condition += " AND PPA.APPLICATION_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpApplicationDate.EditValue).ToString("yyyy-MM-dd") + " 00:00:00')";
            }
            if (dtpApplicationDateTo.EditValue != null)
            {
                condition += " AND PPA.APPLICATION_DATE <= CONVERT(DATETIME,'" + ((DateTime)dtpApplicationDateTo.EditValue).ToString("yyyy-MM-dd") + " 23:59:59')";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "APPLICATION_DATE DESC";
            DataTable dt = HrPersonPositionApplicationBs.GetByCondition(condition, orderBy, rowCount);
            devDataGridView1.DataSource = dt;
        }
        private void ResetButtonsEnable()
        {
            btnModify.Enabled = true;//暂设true 调试
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            if (mSelectedPersonPositionApp == null)
            {
                return;
            }
            if (mSelectedPersonPositionApp.Status == "STATUS01" || mSelectedPersonPositionApp.Status == "STATUS02")
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedPersonPositionApp.Status == "STATUS04")
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            mCanQueryOtherPerson = UICommon.UICommonFunctions.CheckPersonRight(RoleCategory.人事);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "SATUS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", true);
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedPersonPositionApp = new HrPersonPositionApplicationEntity(dr);
            ResetButtonsEnable();
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtDepartment.Tag = frm.SelectedDepartment;
                txtDepartment.Text = frm.SelectedDepartment.DepartmentName;
            }
            else
            {
                txtDepartment.Tag = null;
                txtDepartment.Text = "";
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Tag = frm.SelectedPerson;
                txtPerson.Text = frm.SelectedPerson.NameCn;
            }
            else
            {
                txtPerson.Tag = null;
                txtPerson.Text = "";
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectedPersonPositionApp == null)
            {
                MessageBox.Show("请选择一个调职申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainEmployeePositionApplication frm = new frmMaintainEmployeePositionApplication(mSelectedPersonPositionApp);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        protected override void Click_New()
        {
            frmMaintainEmployeePositionApplication frm = new frmMaintainEmployeePositionApplication();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Delete()
        {
            if (mSelectedPersonPositionApp == null)
            {
                MessageBox.Show("请选择一个调职申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中调职申请单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = HrPersonPositionApplicationBs.DeleteHrPersonPositionApplication(mSelectedPersonPositionApp);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedPersonPositionApp == null)
            {
                MessageBox.Show("请选择一个调职申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainEmployeePositionApplication frm = new frmMaintainEmployeePositionApplication(mSelectedPersonPositionApp);
            frm.ThisActionType = ActionType.Modify;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedPersonPositionApp == null)
            {
                MessageBox.Show("请选择一个调职申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainEmployeePositionApplication frm = new frmMaintainEmployeePositionApplication(mSelectedPersonPositionApp);
            frm.ThisActionType = ActionType.Approve;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }


        #endregion
    }
}
